import prompt from '@system.prompt';
import router from '@system.router';
import http from '@ohos.net.http';
import {expandsList,incomesList} from '../../common/iconInfo.js';
export default {
    data: {
        zeroFlag: 0,
        oneFlag: 1,
        userId: 1,
        titleList: ['支出', '收入'],
        contentList: ['First', 'Second'],
        newDate:'',
        tabsIndex:0,
        flag: 0,
        sort: -1,
        billId: -1,
        amount: null,
        allList: [],
    },
    /*
        初始化选择分类列表和选择器时间
     */
    onInit() {
        this.newDate = JSON.stringify(new Date()).substring(1, 11);
        if(this.amount > 0){
            this.flag = this.oneFlag
            this.allList = incomesList;
        }else if(this.amount < 0){
            this.amount = -this.amount
            this.flag = this.zeroFlag
            this.allList = expandsList;
        }else{
            this.amount = null;
            this.falg = 0;
            this.allList = expandsList;
        }
        if (this.sort !== -1) this.allList[this.sort].choose = true;
    },
    /*
        点击tab标签页(0,1)，清空sort信息(sort,choose),更换allList列表信息
     */
    changeFlag(e) {
        if(this.sort !== -1) this.allList[this.sort].choose = false;
        this.sort = -1;
        if (e.index === this.zeroFlag) {
            this.flag = this.zeroFlag
            this.allList = expandsList;
        } else if (e.index === this.oneFlag) {
            this.flag = this.oneFlag
            this.allList = incomesList;
        }
    },
    /*
       点击时间选择器，修改时间
     */
    changeDate(e) {
        this.newDate = e.year + '-' + (e.month + 1) + '-' + e.day;
        prompt.showDialog({
            message: '是否继续添加账单?',
            buttons: [
                {
                    text: '继续添加',
                    color: ''
                },
                {
                    text: '返回账单',
                    color:''
                }
            ],
            success: (data) =>{
                console.log('handling callback',data.index)
                if(data.index == 1){
                    router.push({
                        uri: "pages/accountBook/accountBook"
                    })
                }
            },
            cancel: function () {
                console.log('handling cancel')
            },
            fail: function (data, code) {
                console.log(`handling fail, code = ${code}`)
            }

        })
    },
    /*
        点击图标，选择账单分类(sort),记录sort值(sort = index)，改变choose状态
     */
    changeSort(index) {
        if(this.sort !== -1){//sort值不为-1，说明之前选择了sort,，需要改变之前choose的状态
            this.allList[this.sort].choose = false;
        }
        this.sort = index;//更新sort值
        this.allList[this.sort].choose = true;
    },
    /*
        点击完成，进行表单提交，先检测数据是否合格(sort,amount)，合格发送数据，不合格不发送并显示弹框信息
     */
    onSubmit(result) {
        this.amount = Number(result.value.amount)
        let note = result.value.note
        if (this.sort !== -1 && this.amount) { //数据存在
            if (this.flag === 0) this.amount = -result.value.amount //支出取反
            let date = this.$element('datepicker').attr.selected
            //发送请求
//            if (submitflag === 1) {
                let httpRequest = http.createHttp();
                httpRequest.request(
                    `http://120.78.198.170:3300/addBill?userId=${this.userId}&time=${date}&amount=${this.amount}&type=${this.sort}&note=${note}&flag=${this.flag}`,
                    {
                        method: 'GET',
                        readTimeout: 60000,
                        connectTimeout: 600
                    }, (err, data) => {
                    if (!err) {//请求成功
                        console.info('Result:' + data.result);
                        console.info('code:' + data.responseCode);
                        prompt.showDialog({
                            message: '是否继续添加账单?',
                            buttons: [
                                {
                                    text: '继续添加',
                                    color: ''
                                },
                                {
                                    text: '返回账单',
                                    color:''
                                }
                            ],
                            success: (data) =>{
                                console.log('handling callback',data.index)
                                if(data.index == 1){
                                    router.back();
                                }
                            },
                            cancel: function () {
                                console.log('handling cancel')
                            },
                            fail: function (data, code) {
                                console.log(`handling fail, code = ${code}`)
                            }
                        })
                    } else {
                        console.info('error:' + err.data);
                    }
                }
                )
//            }
        }
        else {//不合格(sort,amount)
            if (this.sort === -1) {
                prompt.showToast({
                    message: '请选择一个分类',
                })
            }
            else if (this.amount == 0) {
                prompt.showToast({
                    message: '请输入金额',
                })
            }
        }
    },
    /*
        重置表单
     */
    onReset() {
        console.log('reset all value')
    },

    /*
        返回记账本页面
     */
    backPage() {
        router.back();
    },
}

//修改账单依旧是这个add页面，但是修改账单  需要先查询账单将之前的数据显示出来？？如何显示金额和备注和choose字段？？ 页面之间需要传值

